package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.sumpu.app.coolsk.dao.KeepDao;
import cn.sumpu.app.coolsk.domain.Keep;
import cn.sumpu.app.coolsk.domain.MemberDetail;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class KeepDaoImpl  extends SqlSessionDaoSupport implements KeepDao{
	
	private static final Logger logger = LoggerFactory.getLogger(MemberDaoImpl.class);
	//收藏
	@Override
	public int insert(Keep keep)throws AppBusinessException {
		
		try { 
			return getSqlSession().insert("cn.sumpu.app.coolsk.persistence.KeepMapper.insert",keep); 
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertKeep database failure.", e);
			}
			throw new AppBusinessException("insertKeep database failure.", e);
		} 
	}
	//我的收藏总数
	@Override
	public int getKeepCountByMid(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.KeepMapper.getKeepCountByMid",
							map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getKeepCountByMid database failure.", e);
			}
			throw new AppBusinessException("getKeepCountByMid database failure.", e);
		}
	}
	//我的收藏
	@SuppressWarnings("unchecked")
	@Override
	public List<Keep> getKeepListByMid(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.KeepMapper.getKeepListByMid",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getKeepListByMid database failure.", e);
			}
			throw new AppBusinessException("getKeepListByMid database failure.", e);
		}
	}
	//
	@Override
	public Keep getKeep(Map<String, Object> map) throws AppBusinessException{
		
		try {
			return (Keep) getSqlSession()
					.selectOne(
							"cn.sumpu.app.coolsk.persistence.KeepMapper.getkeepByMap",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getkeep failure.", e);
			}
			throw new AppBusinessException("getkeep failure.", e);
		}
	}
	@Override
	public void deleteKeep(Map<String,Object> map)throws AppBusinessException {
		
		try { 
			 getSqlSession().delete("cn.sumpu.app.coolsk.persistence.KeepMapper.deleteKeep",map); 
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteKeep database failure.", e);
			}
			throw new AppBusinessException("deleteKeep database failure.", e);
		} 
	}
}
